<template>
  <div>
    <!-- 页面,面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>数据统计</el-breadcrumb-item>
      <el-breadcrumb-item>数据报表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 白色背景小卡片 -->
    <el-card class="box-card">
      <div id="myecharts"></div>
    </el-card>
  </div>
</template>

<script>
import * as echarts from 'echarts';
import _ from 'lodash';

export default {
  // mounted里边去挂载,dom树已经存在
  async mounted () {
    const chartDom = document.getElementById('myecharts');
    const myChart = echarts.init(chartDom);
    const { data: { data, meta } } = await this.$axios.get('reports/type/1')
    if (meta.status !== 200) {
      return this.$message.error('获取报表数据失败了')
    }
    console.log(data, this.options)
    // es6 将data的数据,放到option里边,相同的属性,后边会覆盖前边的
    // Object.assign(this.options, data);
    const ret = _.merge(data, this.options)
    console.log(ret)
    myChart.setOption(ret);
  },
  data () {
    return {
      // 需要合并的数据,返回的数据不全面
      options: {
        title: {
          text: '用户来源'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#E9EEF3'
            }
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            boundaryGap: false
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ]
      }
    }
  }
}
</script>

<style lang="css" scoped>
#myecharts {
  width: 80%;
  height: 50%;
  min-width: 600px;
  min-height: 400px;
}
</style>